package com.tracesystem.commons.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.tracesystem.util.CommonsUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
/**
 * <p>
 * 自动填充创建时间 创建人 修改时间 修改人
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictUpdateFill(metaObject, "modifyTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "createBy", () -> CommonsUtils.getLoginUserInfo().getUsername(), String.class);
        this.strictUpdateFill(metaObject, "modifyBy", () -> CommonsUtils.getLoginUserInfo().getUsername(), String.class);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "modifyTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictUpdateFill(metaObject, "modifyBy", () -> CommonsUtils.getLoginUserInfo().getUsername(), String.class);
    }
}
